<template>
  <view>
    <!-- 渲染选择收货地址 -->
    <view class="address-choose-box" v-if="JSON.stringify(address) === '{}'">
      <button type="primary" size="mini" class="btnChooseAddress" @click="chooseAddress">添加收货地址+</button>
    </view>

    <!-- 渲染收货信息 -->
    <view class="address-info-box" v-else @click="chooseAddress">
      <view class="row1">
        <view class="left">
          <view class="username">
            收货人：{{address.userName}}
          </view>
        </view>
        <view class="right">
          <view class="phone">
            电话：{{address.telNumber}}
          </view>
          <uni-icons type="right" size="16"></uni-icons>
        </view>
      </view>
      <view class="row2">
        <view class="left">
          收货地址：
        </view>
        <view class="right">
          {{addr_detail}}
        </view>
      </view>
    </view>
    <image src="../../static/cart_border@2x.png" class="address-border"></image>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    name: "my-address",
    data() {
      return {};
    },

    methods: {
      ...mapMutations('m_user', ['updateAddress']),
      async chooseAddress() {
        const [err, success] = await uni.chooseAddress().catch(err => err)
        if (err === null && success.errMsg === 'chooseAddress:ok') {
          this.updateAddress(success)
        }
      },
    },
    computed: {
      ...mapState('m_user', ['address']),
      ...mapGetters('m_user', ['addr_detail'])
    }
  }
</script>

<style lang="scss">
  .address-border {
    display: block;
    width: 100%;
    height: 10rpx;
  }

  .address-choose-box {
    display: flex;
    height: 180rpx;
    justify-content: center;
    align-items: center;

    .btnChooseAddress {}
  }

  .address-info-box {
    font-size: 30rpx;
    height: 180rpx;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 10rpx;

    .row1 {
      display: flex;
      justify-content: space-between;

      .left {
        .username {}
      }

      .right {
        display: flex;

        .phone {}
      }
    }

    .row2 {
      display: flex;
      align-items: center;
      margin-top: 20rpx;

      .left {
        white-space: nowrap;
      }

      .right {}
    }
  }
</style>
